/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package casino;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

/**
 * Class for a player that bets on a randomly chosen outcome.
 * 
 * @author amaghfur
 */
public class PlayerRandom extends Player {
    
    /** random number generator to be used in picking outcomes to bet on */
    private Random rng;
    /** list of all possible outcomes in game */
    private ArrayList<Outcome> all_OC = new ArrayList<>();
    /** amount to always bet */
    private Rational betAmount = new Rational(10);
    
    /**
     * Class constructor for this player that bets on a randomly chosen outcome.
     * 
     * @param tableToPlay
     * @param wheelToPlay
     * @param setRng 
     */
    PlayerRandom(Table tableToPlay, Wheel wheelToPlay, Random setRng) {
        super(tableToPlay);
        rng = setRng;
        
        // build the possible ways to place bets
        Iterator<Bin> itBin = wheelToPlay.binIterator();
        while(itBin.hasNext() == true) {
            Iterator<Outcome> itOut = itBin.next().outcomeIterator();
            while(itOut.hasNext() == true)
                all_OC.add(itOut.next());
        }
    }
    
    /**
     * Get this player to place bet on a randomly chosen outcome.
     */
    @Override
    public void placeBets() {
        int rand = rng.nextInt(all_OC.size());
        Outcome randOutcome = all_OC.get(rand);
                
        Bet blackBet = new Bet(betAmount, randOutcome);
        setStake(getStake().minus(betAmount));
        
        try {
            getTable().placeBet(blackBet);
        }
        catch (InvalidBetException e) {
            System.err.println("Caught InvalidBetException: " + e.getMessage());
        }
    }
    
    /**
     * Notifies this player that his bet specified on the argument is a winner and
     * by how much.
     * 
     * @param bet   winning bet
     */
    @Override
    public void win(Bet bet) {
        setStake(getStake().plus(bet.winAmount()));
        System.out.println(bet.toString() + " -> won " + bet.winAmount().toString());
    }
    
    /**
     * Notifies this player that his bet specified on the argument is a loser and
     * by how much.
     * 
     * @param bet   losing bet
     */
    @Override
    public void lose(Bet bet) {
        System.out.println(bet.toString() + " -> lost " + bet.loseAmount().toString());
    }
    
    /**
     * Inherited abstract method not important for this subclass.
     * 
     * @param outcomes 
     */
    @Override
    public void winners(Set<Outcome> outcomes) {
    }
    
    /**
     * No need to implement this abstract method.
     */
    @Override
    public void reset() {
    }
    
}
